//
//  TrendViewController.m
//  CaiPiao
//
//  Created by apple on 2018/5/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TrendViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface TrendViewController ()<UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *trendWebView;
@property (weak, nonatomic) IBOutlet UIView *buttonBoxView;




@property (weak, nonatomic) IBOutlet UIView *boardView;

@end

@implementation TrendViewController{
    NSString* currentURLString;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"走势";
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
    longPress.delegate = self; //记得在.h文件里加上委托
    longPress.minimumPressDuration = 0.3; //这里为什么要设置0.4，因为只要大于0.5就无效，我像大概是因为默认的跳出放大镜的手势的长按时间是0.5秒，
    //如果我们自定义的手势大于或小于0.5秒的话就来不及替换他的默认手势了，这是只是我的猜测。但是最好大于0.2秒，因为有的pdf有一些书签跳转功能，这个值太小的话可能会使这些功能失效。
    
    
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [EasyLodingView hidenLoding:self.allLoadingView];
//    });
    
    [self loadLottoryTrendWith:@"http://news.cp.lovedou.com/lotteryChart?lottery=7&type=trend"];
    [EasyLodingView showLodingText:@"正在努力加载中..."];
    self.boardView.hidden = YES;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        [self loadLottoryTrendWith:@"http://news.cp.lovedou.com/lotteryChart?lottery=7&type=trend"];
//    });
    
//    福彩:https://caipiao.163.com/t/trend/x3d/
//    大乐透：https://trend.caipiao.163.com/dlt/
//    快三：http://caipiao.163.com/t/trend/jskuai3/
//    江苏11选5：https://news.cp.lovedou.com/lotteryChart?lottery=14&type=trend
//    排列三：https://caipiao.163.com/t/trend/pl3/
//    11X5:http://news.cp.lovedou.com/lotteryChart?lottery=7&type=trend
//    夺金11选5:http://news.cp.lovedou.com/lotteryChart?lottery=8&type=trend

}

-(void)loadLottoryTrendWith:(NSString*)urlString{
    self.boardView.hidden = YES;
//    self.trendWebView.hidden = YES;
    [self.trendWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}

- (IBAction)selectedStyleAction:(UIButton *)sender {
    self.boardView.hidden = YES;
    for (UIView* view in self.buttonBoxView.subviews) {
        if (view.tag>100) {
            UIButton* styleButton = (UIButton*)view;
            [styleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    NSString* urlString = @"";
    switch (sender.tag) {
        case 101:
//            双色球
            urlString = @"https://caipiao.163.com/t/trend/ssq/";
            break;
        case 102:
            //    福彩
            urlString = @"https://caipiao.163.com/t/trend/x3d/";
            break;
        case 103:
            //    大乐透
            urlString = @"https://caipiao.163.com/t/trend/dlt/";
            break;
        case 104:
            //    快三
            urlString = @"https://caipiao.163.com/t/trend/jskuai3/";
            break;
        case 105:
            //    江苏11选5
            urlString = @"https://news.cp.lovedou.com/lotteryChart?lottery=14&type=trend";
            break;
        case 106:
            //    排列三
            urlString = @"https://caipiao.163.com/t/trend/pl3/";
            break;
        case 107:
            //    11X5
            urlString = @"https://news.cp.lovedou.com/lotteryChart?lottery=7&type=trend";
            break;
        case 108:
            //    夺金11选5
            urlString = @"https://news.cp.lovedou.com/lotteryChart?lottery=8&type=trend";
            break;
        default:
            break;
    }
    currentURLString = urlString;
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [EasyLodingView hidenLoding:self.allLoadingView];
//    });
//    [self loadLottoryTrendWith:urlString];
    [EasyLodingView showLodingText:@"正在努力加载中..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self loadLottoryTrendWith:urlString];
    });
}

-(void)viewDidAppear:(BOOL)animated{
    kLogClass
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [EasyLodingView hidenLoding:self.allLoadingView];
//    });
}

-(void)viewWillDisappear:(BOOL)animated{
    kLogClass
    [self.trendWebView stopLoading];
    [self.trendWebView removeFromSuperview];
}


#pragma mark - GestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return NO; //这里一定要return NO,至于为什么大家去看看这个方法的文档吧。
    //还有就是这个委托在你长按的时候会被多次调用，大家可以用nslog输出gestureRecognizer和otherGestureRecognizer
    //看看都是些什么东西。
}



#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    kLogClass
    if ([request.URL.absoluteString hasPrefix:@"https://www.qipaigame1.com/"]) {
        return NO;
    }
    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    kLogClass
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.trendWebView.hidden = NO;
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('esDialog0').style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('pageHead').style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-carousel-wrap topHiPos2')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-banner')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-bottomRight')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('footer-lottery')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('footer-source')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('footer-copy')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('es-dialog-wrap')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('es-dialog-body m-dialog-body')[0].style.display = 'NONE'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('m-freeBonus')[0].style.display = 'NONE'"];
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.boardView.hidden = YES;
    });

    [EasyLodingView hidenLoding];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
   kLogClass
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
